package com.yu1996.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class GulimallCorsConfigration {


    /**
    *Description: 配置网关跨域问题
    *Date: 2021/8/20
    *Author: yu
    *Return: spring封装的跨域配置对象
    */
    @Bean
    public CorsWebFilter getCorsWebFilter(){
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        List<String> allow = new ArrayList<>();
        allow.add("*");
        corsConfiguration.setAllowedHeaders(allow);
        corsConfiguration.setAllowedMethods(allow);
        corsConfiguration.setAllowedOrigins(allow);
        corsConfiguration.setAllowCredentials(true);
        corsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(corsConfigurationSource);
    }
}
